(function () {
    var game = window.game = {
            width: 0,
            height: 0,

            asset: null,
            stage: null,
            ticker: null,
            clickScreen: null,
            bg: null,

            init: function () {
                this.asset = new game.Asset();
                this.asset.on('complete', function (e) {
                    this.asset.off('complete');
                    this.initStage();
                }.bind(this));
                this.asset.load();
            },

            initStage: function () {


                this.width = maxWidth;
                this.height = maxHeigth;
                this.scale = scale;

                //鑸炲彴
                this.stage = new Hilo.Stage({
                    renderType: 'canvas',
                    width: this.width,
                    height: this.height,
                    scaleX: this.scale,
                    scaleY: this.scale
                });
                document.getElementById("g1").appendChild(this.stage.canvas);

                this.ticker = new Hilo.Ticker(60);
                this.ticker.addTick(Hilo.Tween);
                this.ticker.addTick(this.stage);
                this.ticker.start();

                this.stage.enableDOMEvent(Hilo.event.POINTER_START, true);

                this.stage.onUpdate = this.onUpdate.bind(this);

                this.initScenes();
            },


            initScenes: function () {

                this.clickScreen = new game.Q1PlayScreen({
                    width: this.width,
                    height: this.height,
                    image: this.asset
                }).addTo(this.stage);
            },
            onUpdate: function (delta) {
            }
        }
    ;

})
();

(function (ns) {
    var Asset = ns.Asset = Hilo.Class.create({
        Mixes: Hilo.EventMixin,

        queue: null,

        bg: null,
        btn: null,
        load: function () {
            var resources = [
                {id: 'bg', src: 'images/daye.jpg'},
                {id: 'btn', src: 'images/btn.png'}
            ];

            this.queue = new Hilo.LoadQueue();
            this.queue.add(resources);
            this.queue.on('complete', this.onComplete.bind(this));
            this.queue.start();
        },

        onComplete: function (e) {
            this.bg = this.queue.get('bg').content;
            this.btn = this.queue.get('btn').content;
            this.queue.off('complete');
            this.fire('complete');
        }
    });
})(window.game);

(function (ns) {

    var ClickScreen = ns.Q1PlayScreen = Hilo.Class.create({
        Extends: Hilo.Container,
        constructor: function (properties) {
            ClickScreen.superclass.constructor.call(this, properties);
            this.init(properties);
        },
        statue: 0,//0:表示初始状态，1：表示成功，2：表示失败
        clickQueue: [],//记录用户点击的数组
        init: function (properties) {
            this.target = [{x: 526, y: 249}, {x: 404, y: 366}, {x: 244, y: 199}, {x: 374, y: 88}];

            this.checkInRange = function (clickX, clickY, x, y, r) {
                if (clickX >= x && clickX <= (x + r) && clickY >= y && clickY <= (y + r)) {
                    return true;
                } else {
                    console.log("导致失败的是：" + clickX)
                    return false;
                }
            }

            var bg = new Hilo.Bitmap({
                if: "bg",
                image: properties.image.bg,
                rect: [0, 0, maxWidth, maxHeigth],
                x: 0,
                y: 0
            });
            this.addChild(bg);


            var reStartBtn = new Hilo.Button({
                id: 'reStartBtn',
                image: properties.image.btn,
                width: 64,
                height: 64,
                upState: {rect: [0, 0, 64, 64]},
                overState: {rect: [64, 0, 64, 64]},
                downState: {rect: [128, 0, 64, 64]},
                disabledState: {rect: [192, 0, 64, 64]},
                visible: false,
                x: 70,
                y: 450
            })

            this.addChild(reStartBtn);

            reStartBtn.on(Hilo.event.POINTER_START, function (e) {
                e._stopped = true;
                reStartBtn.visible = false;
                this.reset();
            }.bind(this))

            bg.on(Hilo.event.POINTER_START, function (e) {
                e._stopped = true;
                if (this.clickQueue.length < 4) {
                    var clickIndex = new Hilo.Text({
                        color: "yellow",
                        font: "35px arial",
                        text: this.clickQueue.length + 1,
                        textVAlign: "middle",
                        x: e.stageX,
                        y: e.stageY
                    });
                    this.clickQueue.push({x: e.stageX, y: e.stageY, ele: clickIndex})
                    this.addChild(clickIndex);
                }
            }.bind(this));
        },

        onUpdate: function () {

            if (this.statue == -1 || this.statue == 1) {
                return;
            }

            if (this.clickQueue && this.clickQueue.length == 4) {
                if (this.statue == -1) {
                    return;
                }
                for (var a = 0; a < this.clickQueue.length; a++) {
                    var click = this.clickQueue[a];
                    var target = this.target[a];
                    if (this.checkInRange(click.x, click.y, target.x, target.y, 120)) {
                        if (this.statue == 0) {
                            this.statue = 1;
                            window.q1Statue = 1;
                            return;
                        } else if (this.statue == -1) {
                            //表示他已经失败了
                            this.statue = -1;
                            window.q1Statue = -1;
                            return;
                        } else {
                            this.statue = 1;
                            window.q1Statue = 1;
                            return;
                        }
                    } else {
                        this.statue = -1;
                        window.q1Statue = -1;
                        this.getChildById("reStartBtn").visible = false;
                        return;
                    }
                }
            }
        },
        reset: function () {

            this.statue = 0;
            this.clickQueue.map(function (ele) {
                this.removeChild(ele.ele);
            }.bind(this))
            this.clickQueue = [];
        }
    });

})(window.game);